package com.yyc.system.controller;
import com.yyc.core.Result;
import com.yyc.core.ResultGenerator;
import com.yyc.enumCollection.PathEnum;
import com.yyc.system.model.TFile;
import com.yyc.system.model.TUser;
import com.yyc.system.service.ITFileService;
import com.yyc.system.service.ITUserService;
import com.yyc.utils.FileUtils;
import com.yyc.utils.poi.POIUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
* Created by yyc on 2019/01/13.
*/
@RestController
@RequestMapping("/t/file")
public class TFileController {
	
	public static final String BASE_URL = PathEnum.FILE_PATH.getPath();
	
    @Autowired
    private ITFileService tFileService;
    @Autowired
    private ITUserService userService;

    @RequestMapping("/add")
    public Result add(@RequestBody TFile tFile) {
        tFileService.save(tFile);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        tFileService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody TFile tFile) {
        tFileService.update(tFile);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        TFile tFile = tFileService.findById(id);
        return ResultGenerator.genSuccessResult(tFile);
    }

    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<TFile> list = tFileService.findAll();
        PageInfo<TFile> pageInfo = new PageInfo<TFile>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    
    //**************************************************************
	/**
	 * 单个文件上传
	 * @param request
	 * @return
	 */
	@RequestMapping("/upload")
	public Result upload(HttpServletRequest request) {
			List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
			Integer userId = Integer.valueOf(request.getParameter("userId"));
			if (files.isEmpty()) {
				return ResultGenerator.genFailResult("文件为空");
			}
			MultipartFile file = files.get(0);
			// 获取文件名
			String fileName = file.getOriginalFilename();
			String fileType = request.getParameter("fileType");// 图片对应类型
			String filePath = BASE_URL+ fileType+"//";
			String relativePath = "fileType//";
			String name = FileUtils.uploadFile(file, filePath);//存储的文件名
			if(StringUtils.isEmpty(name)) {
				return ResultGenerator.genFailResult("上传失败");
			}
			TFile tFile = new TFile();
			tFile.setFileName(fileName);
			tFile.setFileUrl(relativePath+name);
			tFile.setUserId(userId);
			tFileService.save(tFile);
			saveUser(filePath+name);
			return ResultGenerator.genSuccessResult("上传成功");
	}
	
	@SuppressWarnings("unchecked")
	public void saveUser(String filePath){
		List<TUser> userList = (List<TUser>) POIUtils.readExcal2Bean(TUser.class, filePath);
		userService.saveList(userList);
	}
}
